//
//  PhotoGalleryViewController.m
//  bApp
//
//  Created by Guglielmo Buzzelli on 19/05/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "PhotoGalleryViewController.h"

@interface PhotoGalleryViewController ()

@end

@implementation PhotoGalleryViewController
@synthesize scrollView;
@synthesize pageControl;
@synthesize thumbScrollView;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

/*-----------------------------SLIDESHOW-------------------------------------------*/    
    pageControlBeingUsed = NO;
    
    NSArray *pics = [NSArray arrayWithObjects:[UIImage imageNamed:@"1_1.jpg"],[UIImage imageNamed:@"2_2.jpg"],
                     [UIImage imageNamed:@"3_3.jpg"],[UIImage imageNamed:@"4_4.jpg"],[UIImage imageNamed:@"5_5.jpg"], nil];
    
    
    
    for (int i = 0; i<pics.count; i++) {
        CGRect frame;
        frame.origin.x = 43 +   self.scrollView.frame.size.width * i;
        frame.origin.y = 120;
        frame.size = CGSizeMake(225, 170);
        
        
        UIImageView *subview =[[UIImageView alloc] initWithFrame:frame];
        subview.image = [pics objectAtIndex:i];
        [self.scrollView addSubview:subview];
        
        
        CGRect pframe;
        pframe.origin.x = 40 +  self.scrollView.frame.size.width * i;
        pframe.origin.y = 113;
        pframe.size = CGSizeMake(245, 190);
        
        UIImage *shadow = [UIImage imageNamed:@"gallery_box_app_empty@2x.png"];
        UIImageView *newIv = [[UIImageView alloc] initWithFrame:pframe ];
        newIv.image = shadow;
        [self.scrollView addSubview:newIv];
        
        /*---------------------------------THUMBNAILS---------------------*/ 
        CGRect thumbframe;
        thumbframe.origin.x = 101 * i;
        thumbframe.origin.y = 5;
        thumbframe.size = CGSizeMake(92, 73);

        UIButton *thumbBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        thumbBtn.frame = thumbframe;
        [thumbBtn setImage:[pics objectAtIndex:i] forState:UIControlStateNormal];
        [thumbBtn addTarget:self action:@selector(buttonPushed:) forControlEvents:UIControlEventTouchUpInside];
        [thumbBtn setTag:i];
        [self.thumbScrollView addSubview:thumbBtn];
        
        
        CGRect thumbPframe;
        thumbPframe.origin.x = 100 * i;
        thumbPframe.origin.y = 3;
        thumbPframe.size = CGSizeMake(100, 80);
        
        UIImageView *thumbnewIv = [[UIImageView alloc] initWithFrame:thumbPframe ];
        thumbnewIv.image = shadow;
        [self.thumbScrollView addSubview:thumbnewIv];
        
        
        
    }
    

    
    self.scrollView.contentSize = CGSizeMake( self.scrollView.frame.size.width * pics.count, self.scrollView.frame.size.height);
    
    [self.view sendSubviewToBack:scrollView];
    
    self.thumbScrollView.contentSize = CGSizeMake(100 *pics.count, self.thumbScrollView.frame.size.height);
    
    [self.view sendSubviewToBack:thumbScrollView];
	
	self.pageControl.currentPage = 0;
	self.pageControl.numberOfPages = pics.count;
    
 /*--------------------------------BACKGROUND----------------------------------------*/       
    UIImageView *backgroundImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"back_ground_app@2x.png"]];
    
    [self.view addSubview:backgroundImage ];
    [self.view sendSubviewToBack:backgroundImage ];
    
   
    
    
    
    
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    // Update the page when more than 50% of the previous/next page is visible
    
    /*
     The page is computed by first starting with self.scrollView.contentOffset.x, which is how far the user has scrolled. Then subtracted from that is the half the width of a page (since the page number actually changes as the user scrolls by the midpoint of a slide). Dividing by the page width and rounding down gives the page count assuming the first page is 0. Since UIPageControl expects the first page to be 1, we add 1 to this count. Setting currentPage on the page control will automatically update the UIPageControl’s UI.
     */
    if (!pageControlBeingUsed) {
        CGFloat pageWidth = self.scrollView.frame.size.width;
        int page = floor((self.scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
        self.pageControl.currentPage = page;
    } 
}

- (void) buttonPushed:(id)sender
{
	
    CGRect frame;
    frame.origin.x = self.scrollView.frame.size.width * [sender tag];
    frame.origin.y = 0;
    frame.size = self.scrollView.frame.size;
    
    [self.scrollView scrollRectToVisible:frame animated:YES ];
    self.pageControl.currentPage = [sender tag];
    
	pageControlBeingUsed = YES;  
    
}



-(IBAction)changePage{
    // update the scroll view to the appropriate page
    
    CGRect frame;
    frame.origin.x = self.scrollView.frame.size.width * self.pageControl.currentPage;
    frame.origin.y = 0;
    frame.size = self.scrollView.frame.size;
    
    [self.scrollView scrollRectToVisible:frame animated:YES ];

	pageControlBeingUsed = YES;    
    
}



-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    pageControlBeingUsed = NO;
}

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    pageControlBeingUsed = NO;
}

- (void)viewDidUnload
{
    [self setScrollView:nil];
    [self setPageControl:nil];
    [self setThumbScrollView:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end
